package com.mxgraph.generatorfunction;

import com.mxgraph.view.mxCellState;

/**
 * The type Mx generator random function.
 *
 * @author Mate  A generator random cost function It will generate random (type "double") edge weights in the range of (<b>minWeight</b>, <b>maxWeight</b>) and rounds the values to <b>roundToDecimals</b>
 */
public class mxGeneratorRandomFunction extends mxGeneratorFunction {
    private double maxWeight = 1;

    private double minWeight = 0;

    private int roundToDecimals = 2;

    /**
     * Instantiates a new Mx generator random function.
     *
     * @param minWeight       the min weight
     * @param maxWeight       the max weight
     * @param roundToDecimals the round to decimals
     */
    public mxGeneratorRandomFunction(double minWeight, double maxWeight, int roundToDecimals) {
        setWeightRange(minWeight, maxWeight);
        setRoundToDecimals(roundToDecimals);
    }

    ;

    public double getCost(mxCellState state) {
        Double edgeWeight = null;

        edgeWeight = Math.random() * (maxWeight - minWeight) + minWeight;
        edgeWeight = (double) Math.round(edgeWeight * Math.pow(10, getRoundToDecimals())) / Math.pow(10, getRoundToDecimals());

        return edgeWeight;
    }

    ;

    /**
     * Gets max weight.
     *
     * @return the max weight
     */
    public double getMaxWeight() {
        return maxWeight;
    }

    ;

    /**
     * Sets weight range.
     *
     * @param minWeight the min weight
     * @param maxWeight the max weight
     */
    public void setWeightRange(double minWeight, double maxWeight) {
        this.maxWeight = Math.max(minWeight, maxWeight);
        this.minWeight = Math.min(minWeight, maxWeight);
    }

    ;

    /**
     * Gets min weight.
     *
     * @return the min weight
     */
    public double getMinWeight() {
        return minWeight;
    }

    ;

    /**
     * Gets round to decimals.
     *
     * @return the round to decimals
     */
    public int getRoundToDecimals() {
        return roundToDecimals;
    }

    ;

    /**
     * Sets round to decimals.
     *
     * @param roundToDecimals the round to decimals
     */
    public void setRoundToDecimals(int roundToDecimals) {
        this.roundToDecimals = roundToDecimals;
    }

    ;
};
